package com.delong.spring.handler.market;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.delong.cache.AppCache;
import com.delong.model.market.Schedule;
import com.delong.service.market.ScheduleService;

@Controller
@RequestMapping("/schedule")
public class ScheduleController {
	private ScheduleService service = new ScheduleService();

	@RequestMapping("/list")
	public String getScheduleList(Map<String, Object> model, ServletRequest req,
			@RequestParam(value = "page_id", defaultValue = "") int page_id) {
		HttpServletRequest request = (HttpServletRequest) req;
		model.put("username", request.getSession().getAttribute("username"));
		model.put("page_id", page_id);
		return "market/Schedule";
	}

	@RequestMapping(value = "/getdata", method = RequestMethod.POST)
	@ResponseBody
	public String getAppListData(Map<String, Object> model,
			@RequestParam(value = "page_id", defaultValue = "") int page_id,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "15") int rows) {

		page = page - 1;
		List<Schedule> elements = this.service.getListElement(page_id, page * rows, rows);
		int total = this.service.getListElementCount(page_id);
		JSONObject result = new JSONObject();
		result.put("rows", elements);
		result.put("total", total);
		return result.toJSONString();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String pageAdd(Map<String, Object> model, @RequestParam(value = "page_id", defaultValue = "0") int paggId,
			@RequestParam(value = "begintime", defaultValue = "") String beginTime,
			@RequestParam(value = "endtime", defaultValue = "") String endTime,
			@RequestParam(value = "id", defaultValue = "0") int id

	) throws ParseException {
		Schedule element = new Schedule();
		element.setPage_id(paggId);
		SimpleDateFormat beginFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		if (!beginTime.trim().equals("")) {

			Date begin = beginFormat.parse(beginTime);
			element.setBegintime(begin);
		}
		if (!endTime.trim().equals("")) {
			Date end = beginFormat.parse(endTime);
			element.setEndtime(end);
		}

		// 分类中的应用数量增加

		if (id == 0) {
			element.setCreatedate(new Date());
			element.setStatus(0);
			service.addElement(element);
		} else {

			element.setId(id);

			this.service.changeElement(element);

		}
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("message", "success");
		return result.toJSONString();
	}

	@RequestMapping("/changestatus")
	@ResponseBody
	public String changePageElementStatus(Map<String, Object> model,
			@RequestParam(value = "id", defaultValue = "0") int id,
			@RequestParam(value = "status", defaultValue = "0") int status,
			@RequestParam(value = "page_id", defaultValue = "0") int pageId) {

		JSONObject result = new JSONObject();
		if (id > 0) {
			if (status == 0) {
				if (this.service.selectValid(pageId)) {
					result.put("code", 1);
					result.put("msg", "已经有合适的排期请将合适排期禁用后在设置");
				} else {
					result.put("code", 0);
					this.service.changeStatus(id, status);
					//页面缓存失效
					AppCache cache = new AppCache();
					cache.deletePages(pageId);
				}
			} else {
				result.put("code", 0);
				this.service.changeStatus(id, status);
			}
		} else {
			result.put("code", 1);
			result.put("msg", "排期失败请联系技术");
		}

		return result.toJSONString();
	}

	@RequestMapping("/getelement")
	@ResponseBody
	public String getElement(Map<String, Object> model, @RequestParam(value = "id", defaultValue = "0") int id) {

		Schedule element = this.service.getElement(id);

		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("data", element);
		return result.toJSONString();
	}

}
